package raceReservationServer.loginGateway;


import authorizationRMI.InvalidUserException;
import authorizationRMI.InvalidPasswordException;
import raceReservationServer.loginGateway.proxies.*;

public class AuthWSGateway implements IAuthorizeGateway {

	/**
	 * Add your code to invoke the Authorization WS here Remember you must use
	 * the stub generated in the proxies package in order to invoke the WS. On
	 * the other hand, REMEMBER that you must catch the generic exception
	 * EXCEPTION AND SEE the contained message in order to map it to the
	 * ValidationException
	 * 
	 */
	
	private AuthorizationWSStub stub;
	private AuthorizationWSStub.Runner  r;
	
	
	public AuthWSGateway(String serviceUri) {
		String[] params = serviceUri.split(" ");
		String url = "http://"+params[0]+":"+params[1]+"/axis2/services/"+params[2]+"?wsdl";
		try{
			stub = new AuthorizationWSStub(url);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	
	public RunnerDO login(String user, String pass) throws InvalidUserException, InvalidPasswordException {
		RunnerDO runner = null;
		try {
			r = stub.login(user, pass);
			System.out.println("Runner: " + r.getName() + " - Age: " + r.getAge() + " - Sex: " + r.getSex());
		} catch (Exception e) {
			System.out.println("Exception Type: " + e.getClass().getSimpleName());
			if (e.getMessage().contains("[InvalidUserException]")) {
				System.out.println("*** NON VALID RUNNER ***");
				System.out.println(e.getMessage());
			}

			if (e.getMessage().contains("[InvalidPasswordException]")) {
				System.out.println("*** INVALID PASSWORD ***");
				System.out.println(e.getMessage());
			}
		}
		runner= new RunnerDO(r.getUsername(), r.getPassword(), r.getName(), r.getAge(), r.getSex());
		return runner;
	}
}
